import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

import request from 'common/request.js'
Vue.prototype.$request = request;

App.mpType = 'app';
Vue.prototype.baseUrl = "http://zhageh5.kacd.top";
Vue.prototype.apiUrl =  "/addons/emoticon/";
Vue.prototype.UserInfo = uni.getStorageSync("userInfo");
Vue.prototype.token = uni.getStorageSync("token");
Vue.prototype.appId = "wxc3bd0fa041f59e0d";

Vue.prototype.CheckUser = function()
{
	this.userInfo = uni.getStorageSync("userInfo");
	this.token = uni.getStorageSync("token");
	
	console.log(this.token)
	
	if(this.token != '')
	{
		this.getUserInfo();
		return false;
	}
	
	if(this.userInfo == '')
	{
		this.Login();
	}
	
}

Vue.prototype.Login = function()
{
	let oUrl = window.location.href;
	//首次进入 没有登录 保存
	var url  = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.appId +
		'&redirect_uri='+this.baseUrl + this.apiUrl +'login/wxOfficialAccountLogin&response_type=code&scope=snsapi_userinfo&state=' +
		oUrl;
	
	window.location.href = url;
	
}

Vue.prototype.getUserInfo = function()
{
	this.$request("user/getUserInfo",{},'POST').then(res=>
	{
		if(res.code == 401)
		{
			this.Login();
			
		}
		else
		{
			uni.setStorageSync('userInfo',res);
			uni.setStorageSync('token',res.token);
		}
		
	})
}


const app = new Vue({
    ...App
})
app.$mount()
